<template>
    <div class="alert-list-container">
        <el-empty description="暂无预警信息" v-if="alerts.length === 0" />

        <el-scrollbar v-else height="calc(100vh - 200px)">
            <div class="alert-item" v-for="alert in alerts" :key="alert.id" :class="{ 'unread': !alert.read }">
                <div class="alert-content">
                    <div class="alert-header">
                        <el-tag :type="getAlertTypeTag(alert.type)" size="small">{{ alert.type }}</el-tag>
                        <span class="alert-time">{{ formatTime(alert.createTime) }}</span>
                    </div>
                    <div class="alert-body">
                        <h4 class="alert-title">{{ alert.title }}</h4>
                        <p class="alert-message">{{ alert.message }}</p>
                    </div>
                    <div class="alert-footer">
                        <div class="alert-source">
                            <el-icon>
                                <Location />
                            </el-icon>
                            <span>{{ alert.location }}</span>
                        </div>
                        <el-button v-if="!alert.read" size="small" type="primary" link
                            @click="handleMarkRead(alert.id)">
                            标记已读
                        </el-button>
                    </div>
                </div>
            </div>
        </el-scrollbar>
    </div>
</template>

<script setup>
import { Location } from '@element-plus/icons-vue'
import { formatDateTime } from '@/utils/date'

const props = defineProps({
    alerts: {
        type: Array,
        required: true,
        default: () => []
    }
})

const emit = defineEmits(['mark-read'])

// 预警类型标签样式
const getAlertTypeTag = (type) => {
    const typeMap = {
        '闯红灯': 'danger',
        '超速': 'warning',
        '逆行': 'primary',
        '设备异常': 'info',
        '其他': 'default'
    }
    return typeMap[type] || 'default'
}

// 格式化时间
const formatTime = (time) => {
    return formatDateTime(time)
}

// 标记已读处理
const handleMarkRead = (alertId) => {
    emit('mark-read', alertId)
}
</script>

<style scoped>
.alert-list-container {
    padding: 10px;
    height: 100%;
}

.alert-item {
    padding: 15px;
    margin-bottom: 10px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
    transition: all 0.3s;
}

.alert-item.unread {
    border-left: 3px solid var(--el-color-primary);
    background-color: #f5f9ff;
}

.alert-item:hover {
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.alert-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.alert-time {
    font-size: 12px;
    color: #999;
}

.alert-title {
    margin: 0 0 8px 0;
    font-size: 15px;
    color: #333;
}

.alert-message {
    margin: 0;
    font-size: 13px;
    color: #666;
    line-height: 1.5;
}

.alert-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px dashed #eee;
}

.alert-source {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #888;
}

.alert-source .el-icon {
    margin-right: 5px;
}
</style>